import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class AutoLogin(object):
    # Chrome请求头
    Chrome_Agent = 'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'

    # selenium是否开启无头浏览器
    Headless_Chrome = False
    chrome_options = Options()
    chrome_options.add_argument("--profile-directory=Default")
    chrome_options.add_argument("--whitelisted-ips")
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--disable-plugins-discovery")

    if Headless_Chrome == True:
        # 此处开启后为无头浏览器
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('blink-settings=imagesEnabled=false')
        chrome_options.add_argument('--disable-gpu')

    chrome_options.add_argument(Chrome_Agent)
    site = 'https://www.amazon.com/'

    def __init__(self):
        pass

    def Selenium(self, username, password):
        print(self.site)
        print('======================================================')
        print('--------------  selenium  start  ---------------------')
        print('======================================================')
        # web = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')

        web = webdriver.Chrome('/usr/local/bin/chromedriver', options=self.chrome_options)
        web.delete_all_cookies()
        web.refresh()
        web.get(self.site)
        web.implicitly_wait(30)
        web.maximize_window()

        # 如VPN速度不快需要开启以下两项
        # print("(5s)页面加载中······")
        # time.sleep(5)

        # 在主页找到登陆链接（由js生成）
        web.find_element_by_xpath("//a[@data-nav-role='signin']").click()

        # 自动输入用户名
        web.find_element_by_xpath("//input[@type='email']").send_keys(username)
        time.sleep(1)
        web.find_element_by_xpath("//input[@id='continue']").click()

        # 自动输入密码
        web.find_element_by_xpath("//input[@type='password']").send_keys(password)
        web.find_element_by_xpath("//input[@name='rememberMe']").click()
        time.sleep(1)
        web.find_element_by_xpath("//input[@id='signInSubmit']").click()

        if web.find_element_by_xpath("//a[@id='resend-approval-link']"):
            input("请到邮箱确认通知")

        # 此处获取登陆Cookie

        getCookies = web.get_cookies()
        getCookies = getCookies[::-1]
        # for cookie in getCookies:
        #     name = cookie.get('name')
        #     value = cookie.get('value')
        #     Cookies += name + "=" + value + "; "

        # TODO 此处业务处理

        with open("amazon-selenium.html", "w") as f:
            f.write(web.page_source)
        print(getCookies)
        print("Selenium Done!")
        print('======================================================')
        print('--------------  selenium  Over  ----------------------')
        print('======================================================')
        # time.sleep(1000)
        web.quit()
        return getCookies

if __name__ == '__main__':
    auto = AutoLogin()
    auto.Selenium("1462648167@qq.com", "201314Aaron")